package com.avalon.shop.service;

import com.avalon.core.exception.AvalonException;
import com.avalon.core.util.JacksonUtil;
import com.avalon.core.util.RestTemplateUtils;
import com.avalon.shop.ShopContext;
import com.avalon.shop.config.ShopConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CartService {
    private final ShopConfig shopConfig;
    private final ShopContext shopContext;
    private final String serviceName = "pet.user.cart";

    public CartService(ShopConfig shopConfig, ShopContext shopContext) {
        this.shopConfig = shopConfig;
        this.shopContext = shopContext;
    }

    public void addCartProduct(Integer productId, Integer userId, Integer count) {
        log.info("Add product {} to cart of user {}", productId, userId);
        HashMap<String, Object> param = new HashMap<>();
        param.put("productId", productId);
        param.put("userId", userId);
        param.put("count", count);
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(
                shopConfig.getServer() + "/pet/cart/add",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
        } else {
            log.error("addCartProduct 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }

    public List<Map> getCartProductByUserId(Integer userId) {
        log.info("get cart product of user {}", userId);
        HashMap<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(
                shopConfig.getServer() + "/pet/cart/get",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            return JacksonUtil.convert2Object(responseEntity.getBody(), new TypeReference<List<Map>>() {
            });
        } else {
            log.error("addCartProduct 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }

    public Integer getCartProductCountByUserId (Integer userId) {
        log.info("get cart product of user {}", userId);
        HashMap<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(
                shopConfig.getServer() + "/pet/cart/get",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            List<Map> maps = JacksonUtil.convert2Object(responseEntity.getBody(), new TypeReference<List<Map>>() {
            });
            Integer sum = 0;
            for (Map map : maps) {
                sum += (Integer) map.get("count");
            }
            return sum;
        } else {
            log.error("addCartProduct 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }

    public void deleteCartProduct(Integer id) {
        log.info("delete cart product {}", id);
        HashMap<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("serviceName", serviceName);
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(
                shopConfig.getServer() + "/model/delete",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
        } else {
            log.error("deleteCartProduct 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }
}
